package Agency.models;
import Agency.views.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;


/**
 *
 * @author wangari
 */
public class DeleteHouseModel  extends javax.swing.JFrame {
   Connection conn= null;
  private ActionListener actionlistener;
 public  DeleteHouse houseToDelete;
 /**
  * constructor that instantiates an object of the view
  */
 public DeleteHouseModel(){
   houseToDelete= new  DeleteHouse(); 
   //houseToDelete.setVisible(true);
 }
 /**
  * closing the view delete house
  */
 public void closePage(){
  houseToDelete.setVisible(false);   
 }
/**
 * getting the house to be removed, and confirming whether its the house to be deleted, 
 * the house is first displayed in a list and then after confirming its the house to delete, you go ahead a nd delete
 */
 public void confirmTheHouse(){
      
    String housePlotNum = houseToDelete.getPlotNumber().getText();
         try {
             
Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 
 java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT * FROM AllHouses WHERE  HousePlotNumber like '%"+housePlotNum+"';");


  while(r.next()) {

      houseToDelete.gettheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );
  }

  System.out.println("OUT OF RESULTSET");
}
       catch (Exception e) {
  JOptionPane.showMessageDialog(null,e);
        }
    }
 
 /**
  * deleting the house from the database as soon its assigned to a tenant, meaning that it has been occupied and
  * its no longer empty
  */
 public void deleteTheHouse(){
    String houseType =houseToDelete.getHouseType().getSelectedItem().toString();
     String housePlotNum = houseToDelete.getPlotNumber().getText();  
     try {
             
Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/MusyiAgency?user=root&password=");
 System.out.println("connected to database");
 
PreparedStatement D =conn.prepareStatement("Delete FROM AllHouses" + " WHERE HousePlotNumber =?");
        D.setString(1,housePlotNum );
        D.execute(); 
 }
       catch (Exception e) {
  JOptionPane.showMessageDialog(null,e);
        }
 }


 }
    
//}
